package control;

import control.bean.ControlBeanConstant;
import control.bean.ControlPropertiesBean;
import ui.FormArea;
import ui.FormPanel;
import ui.FormRow;
import ui.FormRowCell;

import javax.swing.*;
import java.awt.*;
import java.awt.event.FocusAdapter;
import java.awt.event.FocusEvent;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;

/**
 * Created with IntelliJ IDEA.
 * User: Deshun
 * Date: 2/8/13
 * Time: 5:18 PM
 * To change this template use File | Settings | File Templates.
 */
public abstract class Control {
//TODO decide to make as interface or not
    protected int idealHeight;
    protected int idealWidth;
    protected int rowNumber;
    protected int positionInRow;
    protected int fieldType;

    protected FormArea formArea;
    protected FormPanel formPanel;
    protected FormRow row;
    protected FormRowCell cell;
    protected JPanel controlPanel;
    protected JPanel propertiesPanel;

    protected JTextField label;
    protected String labelText;

    //Common items for properties panel
    protected JLabel fieldProperties;
    protected JLabel controlLabel;
    protected JTextField controlName;
    protected JCheckBox hint;
    protected JCheckBox required;
    protected JCheckBox readOnly;

    protected ControlPropertiesBean controlPropertiesBean;

    //TODO fix all panels for each control
    //TODO get ready all special "control types" ie. divisional line, matrix, header, page break
    public abstract Component buildControlPanel();

    public abstract Component buildPropertiesPanel();

    public Component controlPanel() {
        return controlPanel;
    }

    public Component propertiesPanel() {
        return propertiesPanel;
    }

    public void showProperties() {
        formArea.showProperties(propertiesPanel);
    }

    public void hideProperties() {
        formArea.hideProperties();
    }

    public int getIdealHeight() {
        return idealHeight;
    }

    public void setIdealHeight(int height) {
        this.idealHeight = height;
    }

    public int getIdealWidth() {
        return idealWidth;
    }

    public void setIdealWidth(int width) {
        this.idealWidth = width;
    }

    public int getRowNumber() {
        return rowNumber;
    }

    public void setRowNumber(int row) {
        this.rowNumber = row;
    }

    public int getPositionInRow() {
        return positionInRow;
    }

    public void setPositionInRow(int positionInRow) {
        this.positionInRow = positionInRow;
    }

    public FormRow getRow() {
        return row;
    }

    public void setRow(FormRow row) {
        this.row = row;
    }

    public FormRowCell getCell() {
        return cell;
    }

    public void setCell(FormRowCell cell) {
        this.cell = cell;
    }

    public FormArea getFormArea() {
        return formArea;
    }

    public void setFormArea(FormArea formArea) {
        this.formArea = formArea;
    }

    public FormPanel getFormPanel() {
        return formPanel;
    }

    public void setFormPanel(FormPanel formPanel) {
        this.formPanel = formPanel;
    }

    class ControlFocusAdaptor extends FocusAdapter {
        public ControlFocusAdaptor() {}

        @Override
        public void focusGained(FocusEvent e) {
            showProperties();
        }

        @Override
        public void focusLost(FocusEvent e) {
//            hideProperties();
        }
    }

    class ControlMouseAdaptor extends MouseAdapter {
        @Override
        public void mousePressed(MouseEvent e) {
            ((Component) e.getSource()).requestFocus();
            showProperties();
        }
    }

    //Information needed
    // - Generic information
    // row numner
    //position in row
    //label name
    //current input value
    //height
    //width
    // - Property information
    // varies for each control type but generally
    // controlname
    // hint
    // required
    // calculated field
    // read only


    public ControlPropertiesBean exportData() {
        controlPropertiesBean = new ControlPropertiesBean();
        controlPropertiesBean.addProperty(ControlBeanConstant.FIELDTYPE, Integer.toString(this.fieldType));
        controlPropertiesBean.addProperty(ControlBeanConstant.ROWNUMBER, Integer.toString(rowNumber));
        controlPropertiesBean.addProperty(ControlBeanConstant.POSITIONINROW, Integer.toString(positionInRow));
        controlPropertiesBean.addProperty(ControlBeanConstant.LABELNAME, label.getText());
        controlPropertiesBean.addProperty(ControlBeanConstant.HEIGHT, Integer.toString(idealHeight));
        controlPropertiesBean.addProperty(ControlBeanConstant.WIDTH, Integer.toString(idealWidth));
        controlPropertiesBean.addProperty(ControlBeanConstant.CONTROLNAME, controlName.getText());
        controlPropertiesBean.addProperty(ControlBeanConstant.HINT, Boolean.toString(hint.isSelected()));
        controlPropertiesBean.addProperty(ControlBeanConstant.REQUIRED, Boolean.toString(required.isSelected()));
        controlPropertiesBean.addProperty(ControlBeanConstant.READONLY, Boolean.toString(readOnly.isSelected()));

        return controlPropertiesBean;
    }

    public void importData(ControlPropertiesBean controlPropertiesBean) {
        try {
            this.fieldType = Integer.parseInt(controlPropertiesBean.getProperty(ControlBeanConstant.FIELDTYPE));
            this.rowNumber = Integer.parseInt(controlPropertiesBean.getProperty(ControlBeanConstant.ROWNUMBER));
            this.positionInRow = Integer.parseInt(controlPropertiesBean.getProperty(ControlBeanConstant.POSITIONINROW));
            this.labelText = controlPropertiesBean.getProperty(ControlBeanConstant.LABELNAME);
            this.label.setText(controlPropertiesBean.getProperty(ControlBeanConstant.LABELNAME));
            this.idealWidth = Integer.parseInt(controlPropertiesBean.getProperty(ControlBeanConstant.WIDTH));
            this.idealHeight = Integer.parseInt(controlPropertiesBean.getProperty(ControlBeanConstant.HEIGHT));
            this.controlName.setText(controlPropertiesBean.getProperty(ControlBeanConstant.CONTROLNAME));
            this.hint.setSelected(Boolean.parseBoolean(controlPropertiesBean.getProperty(ControlBeanConstant.HINT)));
            this.required.setSelected(Boolean.parseBoolean(controlPropertiesBean.getProperty(ControlBeanConstant.REQUIRED)));
            this.readOnly.setSelected(Boolean.parseBoolean(controlPropertiesBean.getProperty(ControlBeanConstant.READONLY)));

        } catch (Exception e ) {
            System.out.println("Failed to import bean");
        }

    }

    public void removeMe() {
        System.out.println("Deleting Row = "+rowNumber+" Position = "+positionInRow);
        formArea.removeControl(rowNumber, this);
    }

}
